<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport" >导入Excel</Button>
        </form>
       
        <input v-model="gameId" placeholder="游戏id" ></input>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns" :width="2500" :item-width="150"></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑game"
        v-model="editGameModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="150">
        <Form-item label="gameId">
            <Input v-model="formEdit.gameId" disabled ></Input>
        </Form-item>
        <Form-item label="balanceType">
            <Input v-model="formEdit.balanceType"></Input>
        </Form-item>
        <Form-item label="freeBalanceType">
            <Input v-model="formEdit.freeBalanceType"></Input>
        </Form-item>
        <Form-item label="payLineNum">
            <Input v-model="formEdit.payLineNum"></Input>
        </Form-item>
        <Form-item label="jackpotCasinoRate">
            <Input v-model="formEdit.jackpotCasinoRate"></Input>
        </Form-item>
        <Form-item label="jackpotMachineUserRate">
            <Input v-model="formEdit.jackpotMachineUserRate"></Input>
        </Form-item>
        <Form-item label="jackpotMachineRate">
            <Input v-model="formEdit.jackpotMachineRate"></Input>
        </Form-item>
        <Form-item label="jackpotFreeUserRate">
            <Input v-model="formEdit.jackpotFreeUserRate"></Input>
        </Form-item>
        <Form-item label="jackpotFreeRate">
            <Input v-model="formEdit.jackpotFreeRate"></Input>
        </Form-item>
        <Form-item label="jackpotBounsUserRate">
            <Input v-model="formEdit.jackpotBounsUserRate"></Input>
        </Form-item>
        <Form-item label="jackpotBounsRate">
            <Input v-model="formEdit.jackpotBounsRate"></Input>
        </Form-item>
        <Form-item label="jackpotMachineUserBase">
            <Input v-model="formEdit.jackpotMachineUserBase"></Input>
        </Form-item>
        <Form-item label="jackpotFreeUserBase">
            <Input v-model="formEdit.jackpotFreeUserBase"></Input>
        </Form-item>
        <Form-item label="jackpotBounsUserBase">
            <Input v-model="formEdit.jackpotBounsUserBase"></Input>
        </Form-item>
        <Form-item label="jackpotId">
            <Input v-model="formEdit.jackpotId"></Input>
        </Form-item>
        <Form-item label="needLevel">
            <Input v-model="formEdit.needLevel"></Input>
        </Form-item>
        <Form-item label="needVipLevel">
            <Input v-model="formEdit.needVipLevel"></Input>
        </Form-item>
        <Form-item label="newUserSpceNum">
            <Input v-model="formEdit.newUserSpceNum"></Input>
        </Form-item>
        <Form-item label="logTableName">
            <Input v-model="formEdit.logTableName"></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getGames, getAllGames, modifyGame, importGames} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      gameId: '',
      editGameModal: false,
      filename: 'game',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      results: '',
      formEdit: {
        gameId: '',
        balanceType: '',
        freeBalanceType: '',
        payLineNum: '',
        jackpotCasinoRate: '',
        jackpotMachineUserRate: '',
        jackpotMachineRate: '',
        jackpotFreeUserRate: '',
        jackpotFreeRate: '',
        jackpotBounsUserRate: '',
        jackpotBounsRate: '',
        jackpotMachineUserBase: '',
        jackpotFreeUserBase: '',
        jackpotBounsUserBase: '',
        jackpotId: '',
        needLevel: '',
        needVipLevel: '',
        newUserSpceNum: '',
        logTableName: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      let tGameId = 0
      if (this.gameId !== '') {
        tGameId = parseInt(this.gameId)
      }

      console.log('tGameId:', tGameId)
      getGames(tGameId, this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let GameVO = getVO('GameVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = GameVO.decode(pv.data[i])
          vo.gameId = vo.gameId.toNumber()
          vo.jackpotCasinoRate = vo.jackpotCasinoRate.toFixed(5)
          vo.jackpotMachineUserRate = vo.jackpotMachineUserRate.toFixed(5)
          vo.jackpotMachineRate = vo.jackpotMachineRate.toFixed(5)
          vo.jackpotFreeUserRate = vo.jackpotFreeUserRate.toFixed(5)
          vo.jackpotFreeRate = vo.jackpotFreeRate.toFixed(5)
          vo.jackpotBounsUserRate = vo.jackpotBounsUserRate.toFixed(5)
          vo.jackpotBounsRate = vo.jackpotBounsRate.toFixed(5)
          this.tableData.push(vo)
        }

        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditGame (index) {
      let data = this.tableData[index]
      this.formEdit.gameId = data.gameId
      this.formEdit.balanceType = data.balanceType
      this.formEdit.freeBalanceType = data.freeBalanceType
      this.formEdit.payLineNum = data.payLineNum
      this.formEdit.jackpotCasinoRate = data.jackpotCasinoRate
      this.formEdit.jackpotMachineUserRate = data.jackpotMachineUserRate
      this.formEdit.jackpotMachineRate = data.jackpotMachineRate
      this.formEdit.jackpotFreeUserRate = data.jackpotFreeUserRate
      this.formEdit.jackpotFreeRate = data.jackpotFreeRate
      this.formEdit.jackpotBounsUserRate = data.jackpotBounsUserRate
      this.formEdit.jackpotBounsRate = data.jackpotBounsRate
      this.formEdit.jackpotMachineUserBase = data.jackpotMachineUserBase
      this.formEdit.jackpotFreeUserBase = data.jackpotFreeUserBase
      this.formEdit.jackpotBounsUserBase = data.jackpotBounsUserBase
      this.formEdit.jackpotId = data.jackpotId
      this.formEdit.needLevel = data.needLevel
      this.formEdit.needVipLevel = data.needVipLevel
      this.formEdit.newUserSpceNum = data.newUserSpceNum
      this.formEdit.logTableName = data.logTableName

      this.editGameModal = true
    },
    onSubmit () {
      if (this.formEdit.gameId === '' || this.formEdit.balanceType === '' || this.formEdit.freeBalanceType === '' || this.formEdit.payLineNum === '' || this.formEdit.jackpotCasinoRate === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }
      if (this.formEdit.jackpotMachineUserRate === '' || this.formEdit.jackpotMachineRate === this.formEdit.jackpotFreeUserRate === '' || this.formEdit.jackpotFreeRate === '' || this.formEdit.jackpotBounsUserRate === '' || this.formEdit.jackpotBounsRate === '' || this.formEdit.logTableName === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }
      if (this.formEdit.jackpotMachineUserBase === '' || this.formEdit.jackpotFreeUserBase === '' || this.formEdit.jackpotBounsUserBase === '' || this.formEdit.jackpotId === '' || this.formEdit.needLevel === '' || this.formEdit.needVipLevel === '' || this.formEdit.newUserSpceNum === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let GameVO = getVO('GameVO')
      var gameVO = new GameVO()

      gameVO.gameId = Number(this.formEdit.gameId)
      gameVO.balanceType = parseInt(this.formEdit.balanceType)
      gameVO.freeBalanceType = parseInt(this.formEdit.freeBalanceType)
      gameVO.payLineNum = parseInt(this.formEdit.payLineNum)
      gameVO.jackpotCasinoRate = Number(this.formEdit.jackpotCasinoRate)
      gameVO.jackpotMachineUserRate = Number(this.formEdit.jackpotMachineUserRate)
      gameVO.jackpotMachineRate = Number(this.formEdit.jackpotMachineRate)
      gameVO.jackpotFreeUserRate = Number(this.formEdit.jackpotFreeUserRate)
      gameVO.jackpotFreeRate = Number(this.formEdit.jackpotFreeRate)
      gameVO.jackpotBounsUserRate = Number(this.formEdit.jackpotBounsUserRate)
      gameVO.jackpotBounsRate = Number(this.formEdit.jackpotBounsRate)
      gameVO.jackpotMachineUserBase = Number(this.formEdit.jackpotMachineUserBase)
      gameVO.jackpotFreeUserBase = Number(this.formEdit.jackpotFreeUserBase)
      gameVO.jackpotBounsUserBase = Number(this.formEdit.jackpotBounsUserBase)
      gameVO.jackpotId = parseInt(this.formEdit.jackpotId)
      gameVO.needLevel = parseInt(this.formEdit.needLevel)
      gameVO.needVipLevel = parseInt(this.formEdit.needVipLevel)
      gameVO.newUserSpceNum = parseInt(this.formEdit.newUserSpceNum)
      gameVO.logTableName = this.formEdit.logTableName

      modifyGame(gameVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.gameId === this.formEdit.gameId) {
              this.tableData[i].balanceType = this.formEdit.balanceType
              this.tableData[i].freeBalanceType = this.formEdit.freeBalanceType
              this.tableData[i].payLineNum = this.formEdit.payLineNum
              this.tableData[i].jackpotCasinoRate = this.formEdit.jackpotCasinoRate
              this.tableData[i].jackpotMachineUserRate = this.formEdit.jackpotMachineUserRate
              this.tableData[i].jackpotMachineRate = this.formEdit.jackpotMachineRate
              this.tableData[i].jackpotFreeUserRate = this.formEdit.jackpotFreeUserRate
              this.tableData[i].jackpotFreeRate = this.formEdit.jackpotFreeRate
              this.tableData[i].jackpotBounsUserRate = this.formEdit.jackpotBounsUserRate
              this.tableData[i].jackpotBounsRate = this.formEdit.jackpotBounsRate
              this.tableData[i].jackpotMachineUserBase = this.formEdit.jackpotMachineUserBase
              this.tableData[i].jackpotFreeUserBase = this.formEdit.jackpotFreeUserBase
              this.tableData[i].jackpotBounsUserBase = this.formEdit.jackpotBounsUserBase
              this.tableData[i].jackpotId = this.formEdit.jackpotId
              this.tableData[i].needLevel = this.formEdit.needLevel
              this.tableData[i].needVipLevel = this.formEdit.needVipLevel
              this.tableData[i].newUserSpceNum = this.formEdit.newUserSpceNum
              this.tableData[i].logTableName = this.formEdit.logTableName
              break
            }
          }
        }
      })
    },
    onImport () {
      // 导入EXCEL
      importGames(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      getAllGames().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let GameVO = getVO('GameVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = GameVO.decode(pv.data[i])
          vo.gameId = vo.gameId.toNumber()
          allTableData.push(vo)
        }
        console.log(allTableData)
        exportExcel(this.filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'gameId',
          key: 'gameId',
          align: 'center',
          fixed: 'left',
          width: 80
        },
        {
          title: 'balanceType',
          key: 'balanceType',
          align: 'center',
          fixed: 'left',
          width: 80
        },
        {
          title: 'freeBalanceType',
          key: 'freeBalanceType',
          align: 'center',
          fixed: 'left',
          width: 80
        },
        {
          title: 'payLineNum',
          key: 'payLineNum',
          align: 'center',
          fixed: 'left',
          width: 80
        },
        {
          title: 'jackpotCasinoRate',
          key: 'jackpotCasinoRate',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotMachineUserRate',
          key: 'jackpotMachineUserRate',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotMachineRate',
          key: 'jackpotMachineRate',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotFreeUserRate',
          key: 'jackpotFreeUserRate',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotFreeRate',
          key: 'jackpotFreeRate',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotBounsUserRate',
          key: 'jackpotBounsUserRate',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotBounsRate',
          key: 'jackpotBounsRate',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotMachineUserBase',
          key: 'jackpotMachineUserBase',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotFreeUserBase',
          key: 'jackpotFreeUserBase',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotBounsUserBase',
          key: 'jackpotBounsUserBase',
          align: 'center',
          width: 150
        },
        {
          title: 'jackpotId',
          key: 'jackpotId',
          align: 'center',
          width: 80
        },
        {
          title: 'needLevel',
          key: 'needLevel',
          align: 'center',
          width: 80
        },
        {
          title: 'needVipLevel',
          key: 'needVipLevel',
          align: 'center',
          width: 80
        },
        {
          title: 'newUserSpceNum',
          key: 'newUserSpceNum',
          align: 'center',
          width: 80
        },
        {
          title: 'logTableName',
          key: 'logTableName',
          align: 'center',
          width: 120
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditGame(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
